<?php
namespace Services\Admin;
use App\Admin\Models\SourceMaterial;
use Common\Models\ViewHomePath;
use Services\ServicesAdminBase;
class StructureServices extends ServicesAdminBase {
    /**
     * 获取所有可用的路由
     */
    public function homepath($params){
        $ViewHomePath = new ViewHomePath();
        $viewHomePath = $ViewHomePath->getList($params);
        return $viewHomePath;
    }


    /**
     * 添加修改路由素材
     */
    public function sourceAddEdit($params){
        $source_material_find_one = new SourceMaterial();
        if (isset($params['id']) && !empty($params['id'])) {
            $source_material_find_one = $source_material_find_one->find_by_id_obj($params['id']);
            if (!$source_material_find_one) {
                return ['status'=>false,'msg'=>'没有找到该路由记录，该路由已经被管理员删除'];
            }
        }
        $viewHomePathDb = new ViewHomePath();
        $viewHomePath = $viewHomePathDb->findFirstById($params['homepath_id']);
        if(!$viewHomePath){
            return ['status'=>false,'msg'=>'没有找到该路由地址'];
        }
        $app_data = "";
        if($params['homePathParams']){
            $json_app_data = [];
            foreach ($params['homePathParams'] as $key=>$value){
                $json_app_data[$value['key']] = $value['value'];
            }
            $app_data = json_encode($json_app_data);
        }
        $source_material_find_one->name          = $params['name'];
        $source_material_find_one->path          = $params['path'];
        $source_material_find_one->type          = $viewHomePath->type;
        $source_material_find_one->app_path      = $viewHomePath->source_path;
        $source_material_find_one->app_data      = $app_data;
        $source_material_find_one->is_tb_atuh    = $params['is_tb_atuh'];
        $source_material_find_one->is_need_login = $params['is_need_login'];
        $source_material_find_one->title         = $params['title'];
        $source_material_find_one->relationId    = $params['relationId'];
        $source_material_find_one->homepath_id  = $params['homepath_id'];
        $source_material_find_one->platform  = implode(',',$params['platform']);
        if (!$source_material_find_one->save()) {
            return ['status'=>false,'msg'=>'修改路由失败'];
        }
        return ['status'=>true,'msg'=>'修改成功'];
    }


    /**
     * 路由详情
     */
    public function itemsSource($params){
        $sourceDb        = new SourceMaterial();
        $source_find_one = $sourceDb->find_by_id_obj($params['id']);
        if (!$source_find_one) {
            return ['status'=>false,'msg'=>'没有找到路由'];
        }
        $source_find_one = $source_find_one->toarray();
        if($source_find_one['platform']){
            $source_find_one['platform'] = explode(',',$source_find_one['platform']);
        }else{
            $source_find_one['platform'] = [];
        }

        $app_data = "";
        //合并值
        if($source_find_one['homepath_id']){
            $ViewHomePathDB = new ViewHomePath();
            $viewHomePath = $ViewHomePathDB->findFirstById($source_find_one['homepath_id']);
            if($viewHomePath){
                $json_app_data = json_decode($source_find_one['app_data'],true);
                $json_arr = $viewHomePath->json ? json_decode($viewHomePath->json,true) : [];
                foreach($json_arr as $key=>$value){
                    $json_arr[$key]['value'] = $json_app_data[$json_arr[$key]['key']];
                }
                $app_data = json_encode($json_arr);
            }
        }
        $source_find_one['app_data'] = $app_data;
        return ['status'=>true,'msg'=>'获取成功','data'=>$source_find_one];
    }
}